// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var leftMenu = require("../leftMenu/leftMenu");
	var topMenu = require("../topMenu/topMenu");
	var footer = require("../footer/footer_main");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var validate = require("validate");
	var tools = require("tools");
	var css_dialog = require("css_dialog");
	var databind = require("databind");
	var html_receivables_detail = require("./html_receivables_detail.html");
	var html_loan_detail = require("./html_loan_detail.html");
	var html_expense_detail = require("./html_expense_detail.html");
	var financeId;
	var type;
	var source;
	var verifiCancelState;
//	var storage = window.sessionStorage;
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var oData = {
		pageIndex: pageIndex,
		pageSize: pageSize,
		totalPageCount: totalPageCount,
		totalRecordCount: totalRecordCount,
		statementBeginDate: "",
		statementEndDate: "",
		statementNumber: "",
		receiptName: "",
		minSourceAmount: "",
		maxSourceAmount: "",
		financeNumber: "",
		verifiCancelState: "",
//		statementSource: "BAIL_ADDITION",
		statementSource:"",
		verifiCancelState: true
	};
	var init = function() {
		type = base.getUrlParam("type");
		financeId = base.getUrlParam("financeId");
		source = eval ("(" + base.getUrlParam("source") + ")");
		verifiCancelState = base.getUrlParam("verifiCancelState");
		oData.verifiCancelState = verifiCancelState;
		footer.init();
		$("#footer").removeClass("none");
		leftMenu.init(5);
		topMenu.init(1);
		router.init();
		$(".table-section").removeClass("none");
		$(".footer").removeClass("none");
		if (type == "capital") {
			oData.financeId = financeId;
			oData.verifiCancelState = verifiCancelState;
			oData.statementSource = source;
		}
//		tools.loadList("inquireDisbursementMoneyStatementPageList.json", $("#disbursementManageBody"), $("#disbursementManageTpl"), oData, function() {
//		});
		tools.loadList("inquireReceiptMoneyStatementPageList.json", $("#receiptManageBody"), $("#receiptManageTpl"), oData, function() {
//			$(".detail").unbind("click").click(function() {
//				//router.route("insuranceManage/insuranceDetail.html?insuranceId=" + $(this).parents("tr").attr("insuranceId") + "&financeId=" + $(this).parents("tr").attr("financeId"));
//			});
		});
		$(".receiptConfirmManage").parent().parent().parent().addClass("leftMenu-open");
		$(".receiptConfirmManage").parent().addClass("leftMenu-active");
		initEvent();
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			location.href = "../login/login.html";
		}
		
		$("#leftMenu9").on("click", ".claimManage", function() {
			router.route("claimManage/claimManage.html");
		});

		$("#leftMenu9").on("click", ".insurance1", function() {
			location.href = "../insuranceManage/insuranceManage.html";
		});

		$("#leftMenu9").on("click", ".insurance2", function() {
			location.href = "../insuranceManage/insuranceManage.html";
		});

		$("#leftMenu9").on("click", ".insurance3", function() {
			location.href = "../insuranceManage/insuranceManage.html";
		});

		$("#leftMenu9").on("click", ".insurance4", function() {
			location.href = "../insuranceManage/insuranceManage.html";
		});
		
		//已完成融资
		$(".endFinanceList").unbind("click").click(function() {
			location.href = "../financing/endFinanceList.html";
		});
		//收货通知查询
		$(".recieveNoticeList").unbind("click").click(function() {
			location.href = "../financing/recieveNoticeList.html";
		});
		//放货查询
		$(".deliveryNoticeList").unbind("click").click(function() {
			location.href = "../financing/deliveryNoticeList.html";
		});
		//放款单查询
		$(".loanNoticeList").unbind("click").click(function() {
			location.href = "../financing/loanNoticeList.html";
		});

		$("#main").on("click", "#receiptSearch", function() {
			pageIndex = 1;
			pageSize = 10;
			totalPageCount = 1;
			totalRecordCount = 0; //分页初始化			
			oData = {
				pageIndex: pageIndex,
				pageSize: pageSize,
				totalPageCount: totalPageCount,
				totalRecordCount: totalRecordCount,
				statementBeginDate: $(".statementBeginDate").val(),
				statementEndDate: $(".statementEndDate").val(),
				statementNumber: $(".statementNumber").val(),
				disbursementName: $(".disbursementName").val(),
				minSourceAmount: $(".minSourceAmount").val(),
				maxSourceAmount: $(".maxSourceAmount").val(),
				financeNumber: $(".financeNumber").val(),
				productType: $(".wrapper select[name=productType]").val()
			};
			tools.loadList("inquireReceiptMoneyStatementPageList.json", $("#receiptManageBody"), $("#receiptManageTpl"), oData, function() {
				$(".detail").unbind("click").click(function() {
					//router.route("insuranceManage/insuranceDetail.html?insuranceId=" + $(this).parents("tr").attr("insuranceId") + "&financeId=" + $(this).parents("tr").attr("financeId"));
				});
			});
		});
		
		$("#main").on("click", ".confirm", function(){
			var $this = $(this);
			dialog.showConfirmDialog("流水确认", "是否确认该条流水？", function(){
				confirmReceiptMoneyStatement($this.parents("tr").attr("moneystatementId"));
			});
		});

		$("#main").on("click", ".detail", function() {
			var statementSourceId = $(this).parents("tr").attr("statementSourceId");
			var statementSource = $(this).parents("tr").attr("statementSource");
			switch (statementSource) {
				case "RECEIVABLES":
					$("#popdialog").html(html_receivables_detail);
					dialog.showDialog($("#receivable_detail_dialog"));
					databind.fillInput($("#receivable_detail_dialog"), {
						action: "inquireReceivablesDetail.json?receivablesId=" + statementSourceId
					});
					databind.disableInput($("#receivable_detail_dialog"));
					$(".sure_yes").unbind("click").click(function() {
						dialog.hideDialog($("#receivable_detail_dialog"));
					});
					
					$(".sure_no").unbind("click").click(function() {
						router.route("financing/receivableNoticeDetail.html?receivablesId=" + statementSourceId);
						dialog.hideDialog($("#receivable_detail_dialog"));
					});
					break;
				case "ABNORMAL_EXPENSE":
					$("#popdialog").html(html_expense_detail);
					dialog.showDialog($("#exception_detail_dialog"));
					tools.inquireCommonSelectorList(9, $(".expenseType"));
					databind.fillInput($("#exception_detail_dialog"), {
						action: "inquireAbnormalExpenseDetail.json?abnormalExpenseId=" + statementSourceId
					});
					databind.disableInput($("#exception_detail_dialog"));
					$(".sure_yes").unbind("click").click(function() {
						dialog.hideDialog($("#exception_detail_dialog"));
					});
					$(".sure_no").unbind("click").click(function() {
						var abnormalId = $("#abnormalId").val();
						dialog.hideDialog($("#exception_detail_dialog"));
						storage.setItem("abnormalId", abnormalId);
						location.href="../exception/exceptionManage.html#exception/exceptionDetailInfo";
					});
					break;
				case "LOAN_NOTICE":
					$("#popdialog").html(html_loan_detail);
					dialog.showDialog($("#loan_detail_dialog"));
					databind.fillInput($("#loan_detail_dialog"), {
						action: "inquireLoanNoticeDetail.json?loanNoticeId=" + statementSourceId
					});
					databind.disableInput($("#loan_detail_dialog"));
					$(".sure_yes").unbind("click").click(function() {
						dialog.hideDialog($("#loan_detail_dialog"));
					});
					
					$(".sure_no").unbind("click").click(function() {
						router.route("financing/loanNotice.html?loanNoticeId=" + statementSourceId);
						dialog.hideDialog($("#loan_detail_dialog"));
					});
					break;
				case "INSURANCE":
					router.route("insuranceManage/insuranceDetail.html?insuranceId=" + statementSourceId);
					break;
				case "FINANCE_AUDIT":
					router.route("main/financeApproveDetail.html.html?financeAuditId=" + statementSourceId);
					break;
				case "BAIL_ADDITION":
					router.route("financing/adjustdepositRecord.html?creditId=" + statementSourceId);	
					break;
				case "BAIL_RETURN":
					router.route("financing/financingDetailInfo.html?financeId=" + statementSourceId);	
					break;
				default:
					break;
			}
		});
	};
	
	//收款确认
	var confirmReceiptMoneyStatement = function(moneyStatementId) {
		base.fnAjax({
			action: "confirmReceiptMoneyStatement.json", //接口名称
			oData: {
				moneyStatementId: moneyStatementId
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					base.alert(1, "确认成功！");
					location.href = "../moneyStatementManage/receiptConfirmManage.html";
				}
			}
		});
	};

	return {
		init: init
	};
});